package com.whfc.common.exception;

import com.whfc.common.result.ResultEnum;

/**
 * @Description:
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/7/22 14:43
 */
public class BizException extends RuntimeException {

    private String code;
    private String msg;

    public BizException() {
    }

    public BizException(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    // 新增：直接使用ResultEnum构造异常
    public BizException(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMessage();
    }

    // 新增：使用ResultEnum和自定义消息构造异常
    public BizException(ResultEnum resultEnum, String customMsg) {
        this.code = resultEnum.getCode();
        this.msg = customMsg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    /**
     * 是否超时错误
     *
     * @return
     */
    public boolean isTimeoutError() {
        return ResultEnum.NETWORK_TIMEOUT.getCode().equals(code);
    }

    /**
     * 是否网络错误
     *
     * @return
     */
    public boolean isNetoworkError() {
        return ResultEnum.NETWORK_ERROR.getCode().equals(code);
    }

    // 新增：静态工厂方法
    public static BizException of(ResultEnum resultEnum) {
        return new BizException(resultEnum);
    }

    public static BizException of(ResultEnum resultEnum, String customMsg) {
        return new BizException(resultEnum, customMsg);
    }

    // 新增：常用异常快捷创建方法
    public static BizException paramError() {
        return new BizException(ResultEnum.PARAM_ERROR);
    }

    public static BizException paramError(String customMsg) {
        return new BizException(ResultEnum.PARAM_ERROR, customMsg);
    }

    public static BizException networkError() {
        return new BizException(ResultEnum.NETWORK_ERROR);
    }

    public static BizException networkTimeout() {
        return new BizException(ResultEnum.NETWORK_TIMEOUT);
    }

    public static BizException failure() {
        return new BizException(ResultEnum.FAILURE);
    }

    public static BizException failure(String customMsg) {
        return new BizException(ResultEnum.FAILURE, customMsg);
    }
}